package cn.itcast.stream;

import cn.itcast.entity.UserEntity;

import java.util.ArrayList;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ListToMap {
    public static void main(String[] args) {
        ArrayList<UserEntity> userEntities = new ArrayList<>();
        userEntities.add(new UserEntity("张三",18));
        userEntities.add(new UserEntity("李四",26));
        userEntities.add(new UserEntity("王二",10));
        userEntities.add(new UserEntity("麻子",28));


        //list集合只有元素值 key  list转换为map集合的情况下  指定key-user
        //对象  username 属性  value user对象
        /*
            map<string(username),UserEntity>
         */
        Stream <UserEntity> stream = userEntities.stream();
        Map collects = stream.collect(Collectors.toMap(new Function<UserEntity, Object>() {

            @Override
            public String apply(UserEntity userEntity) {
                return userEntity.getName();
            }
        }, new Function<UserEntity, Object>() {
            @Override
            public UserEntity apply(UserEntity userEntity) {
                return userEntity;
            }
        }));

        collects.forEach(new BiConsumer() {
            @Override
            public void accept(Object o, Object o2) {
                System.out.println(o+","+o2);
            }
        });

        System.out.println("---------------------------------------------------------------");
        System.out.println("改为lambda表达式写  ");
        Map collects1 = stream.collect(Collectors.toMap((Function<UserEntity, Object>) userEntity -> userEntity.getName(), (Function<UserEntity, Object>) userEntity -> userEntity));

        collects1.forEach((o, o2) -> System.out.println(o+","+o2));



    }
}
